//LCR 033. 字母异位词分组
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> hash;
        for (auto& s : strs) {
            string temp = s;
            sort(temp.begin(), temp.end());
            hash[temp].emplace_back(s);
        }
        vector<vector<string>> ret;
        for (auto& [k, v] : hash) {
            ret.emplace_back(v);
        }
        return ret;
    }
};

//最长公共前缀
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string ret;
        char temp;
        for (int j = 0; j < strs[0].size(); j++) {
            temp = strs[0][j];
            for (int i = 1; i < strs.size(); i++) {
                if (j >= strs[i].size() || temp != strs[i][j])
                    return ret;
            }
            ret += temp;
        }
        return ret;
    }
};

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string ret = strs[0];
        for (int i = 0; i < strs.size(); i++) {
            int count = 0;
            for (int j = 0; j < strs[i].size(); j++) {
                if (j < ret.size() && strs[i][j] == ret[j])
                    count++;
                else
                    break;
            }
            ret.resize(count);
        }
        return ret;
    }
};